package speed2010.matcher.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import speed2010.matcher.lematching.enums.EMatcher;
import speed2010.matcher.semanticmatching.basics.ESSMFunction;
import speed2010.matcher.util.OntologyMatcherProperties;

public class OntologyMatcherProperties {
	
	public static boolean DEBUG;	
	public static boolean NORMALIZED;
	
	public static String SPEED_URI;

	//Database info
	public static String DB_URL;
	public static String DB_USER;
	public static String DB_PASSWD;
	public static String DB;
	public static String DB_DRIVER;

	//Semantic matching options
	public static int THRESHOLD;	
	public static int THRESHOLD_ROOT;
	public static double EQUIVALENT_VALUE;	
	public static double GENERALIZATION_VALUE;	
	public static double AGGREGATION_VALUE;	
	public static double CLOSENESS_VALUE;
	
	//SL matching options
	public static EMatcher MATCHER;
	public static String MATCHINGS_LE;
	
	//SSM options
	public static double LE_WEIGHT;
	public static double SEMANTIC_WEIGHT;
	
	//Directory options
	public static String FILE_FOLDER; 
	public static String LE_FILENAME;
	public static String FINALALIGN_FILENAME;
	public static String SEMANTIC_FILENAME;
	
	// Other
	public static ESSMFunction SSM_FUNCTION;
	
	
	static {
		loadProperties();
		loadOptions();
	}
		
	public static void loadProperties()
	{
		Properties props = getProperties();
		DB = props.getProperty("db");
		DB_DRIVER = props.getProperty("db_driver");
		DEBUG = Boolean.parseBoolean(props.getProperty("debug"));
		NORMALIZED = Boolean.parseBoolean(props.getProperty("normalized"));
	}
	
	public static Properties getProperties()
	{
		File file = new File("ontologymatcher.properties");
		Properties props = new Properties();
		try {
			props.load(new FileInputStream(file));	
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}	
		
		return props;
	}
	
	public static void loadOptions()
	{
		File file = new File("options.properties");
		Properties props = new Properties();
		try {
			props.loadFromXML(new FileInputStream(file));
			
			SPEED_URI = props.getProperty("speed_uri");
			DB_URL = props.getProperty("db_url");
			DB_USER = props.getProperty("db_user");
			DB_PASSWD = props.getProperty("db_passwd");
			THRESHOLD = Integer.parseInt(props.getProperty("threshold"));
			THRESHOLD_ROOT = Integer.parseInt(props.getProperty("thresholdRoot"));
			LE_WEIGHT = Double.parseDouble(props.getProperty("le_weight"));
			SEMANTIC_WEIGHT = Double.parseDouble(props.getProperty("semantic_weight"));
			MATCHER = EMatcher.create(Integer.parseInt(props.getProperty("matcher")));

			if(props.containsKey("matchingsLE"))
				MATCHINGS_LE = props.getProperty("matchingsLE");
			
			if(props.containsKey("equivalent_value"))
				EQUIVALENT_VALUE = Double.parseDouble(props.getProperty("equivalent_value"));	
			else
				EQUIVALENT_VALUE = 1.0;
			
			if(props.containsKey("generalization_value"))
				GENERALIZATION_VALUE = Double.parseDouble(props.getProperty("generalization_value"));	
			else
				GENERALIZATION_VALUE = 0.8;
			
			if(props.containsKey("composition_value"))
				AGGREGATION_VALUE = Double.parseDouble(props.getProperty("composition_value"));	
			else
				AGGREGATION_VALUE = 0.3;
			
			if(props.containsKey("closeness_value"))
				CLOSENESS_VALUE = Double.parseDouble(props.getProperty("closeness_value"));
			else
				CLOSENESS_VALUE = 0.7;
			
			File tmp = new File (props.getProperty("fileFolder"));
			
			if(tmp.exists())
				FILE_FOLDER = props.getProperty("fileFolder");
			else
				FILE_FOLDER =  System.getProperty("user.dir").replace('\\', '/') + "/tmp/";
			
			LE_FILENAME = props.getProperty("leFilename");
			FINALALIGN_FILENAME = props.getProperty("finalAlignFilename");	
			SEMANTIC_FILENAME = props.getProperty("semanticFilename");
			
			SSM_FUNCTION = ESSMFunction.create(Integer.parseInt(props.getProperty("ssm_function")));
			
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	public static Properties getOptions()
	{
		File file = new File("options.properties");
		Properties props = new Properties();
		try {
			props.loadFromXML(new FileInputStream(file));
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		return props;
	}
	
	public static void saveOptions(Properties props)
	{
		try {
			File file = new File("options.properties");
			props.storeToXML(new FileOutputStream(file), "-- Ontology Matcher Options --");
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	
	public static void saveOption(String prop, String value)
	{
		Properties props = OntologyMatcherProperties.getOptions();
		props.setProperty(prop, value);
		OntologyMatcherProperties.saveOptions(props);
		OntologyMatcherProperties.loadOptions();
	}
	
	public static String getProjectDirectory()
	{
		return System.getProperty("user.dir").replace('\\', '/') + "/";
	}
	
}
